import subprocess
import os


def run_git_command(command):
    try:
        result = subprocess.run(command, shell=True, check=True, text=True, capture_output=True)
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"执行命令 {command} 时出错:")
        print(e.stderr)


def main():
    # 获取脚本所在的目录并切换到该目录
    script_dir = os.path.dirname(os.path.abspath(__file__))
    os.chdir(script_dir)

    choice = input("请选择操作：1. 创建新仓库 2. 更新已有仓库: ")
    if choice == '1':
        remote_url = input("请输入远程仓库地址: ")
        commit_message = input("请输入提交注释: ")
        # 初始化 Git 仓库
        run_git_command("git init")
        # 添加远程仓库
        run_git_command(f"git remote add origin {remote_url}")
    elif choice == '2':
        commit_message = input("请输入提交注释: ")
    else:
        print("无效的选择，请输入 1 或 2。")
        return

    # 添加所有文件到暂存区
    run_git_command("git add --all")

    # 提交更改
    run_git_command(f'git commit -m "{commit_message}"')

    # 推送到远程仓库
    run_git_command("git push -u origin master")


if __name__ == "__main__":
    main()
    